const {
  uniIdScoresCollection
} = require('../../../common/constants')
const {
  ERROR
} = require('../../../common/error')

/**
 * 新增积分信息
 * @param {Object}  params
 * @param {String}  params.user_id     		用户id
 * @param {String}  params.score     		本次变化的积分
 * @param {String}  params.balance     		变化后的积分余额
 * @param {String}  params.type     		积分类型 1:收入 2:支出
 * @param {String}  params.comment     		备注，说明积分新增、消费的缘由
 * @param {String}  params.create_date  	创建时间
 */
module.exports = async function (params = {}) {
  const schema = {
	user_id: 'string',
    score: {
      required: true,
      type: 'number'
    },
	balance: {
	  required: true,
	  type: 'number'
	},
	type: {
	  required: false,
	  type: 'number'
	},
	comment: {
	  required: false,
	  type: 'string'
	},
	create_date: {
	  required: true,
	  type: 'number'
	}
  }
  
  this.middleware.validate(params, schema)
  
  const {
	  user_id,
	  score,
	  balance,
	  type,
	  comment,
	  create_date
  } = params
  
  // 数据
  const data = {
	user_id,
	score,
	balance,
	type,
	comment,
	create_date
  }
  
  const resp = await uniIdScoresCollection.add(data)
  return {
    errCode: 0,
    userRecord: resp
  }
}
